package com.jsyh.maidu.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewStub;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.model.CommentModel;
import com.jsyh.maidu.model.GoodsInfoModel2;
import com.jsyh.maidu.presenter.CommentListPreserter;
import com.jsyh.maidu.utils.DensityUtils;
import com.jsyh.maidu.views.CommentView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class DetailsFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener, CommentView {

    private String encoding = "utf-8";    //编码


    public static final String PARAMS1 = "html";
    public static final String PARAMS2 = "base_params";

    private String mHtmlString;                //webview 加载的文本数据

    private List<GoodsInfoModel2.Other> mBaseParamsDatas;        //基本数据

    private List<CommentModel> mCommentdates;


    private WebView mWebView;
    private String url = "file:///android_asset/jingdong/index/index.html";

    private RadioGroup mSwitchLayoutRadio;

    private RadioButton mIntroductionRadioButton;
    private RadioButton mBaseParamsRadioButton;
    private RadioButton mCommentButton;


    private ViewStub mIntroductionStub;        //商品简介
    private View mIntroductionView;


    private ViewStub mBaseParamsStub;        //基本参数
    private View mBaseParamsView;

    private ViewStub mCommentStub;//评论
    private View mCommentView;

    private String goodsid;

    private TableLayout mParamsTableLayout;

    private CommentListPreserter commentpareserter;

    public static DetailsFragment newInstance(String html, ArrayList<GoodsInfoModel2.Params> param, List<CommentModel> comment) {

        DetailsFragment detatileFragment = new DetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PARAMS1, html);
        bundle.putSerializable(PARAMS1, param);
        bundle.putSerializable("comment", (Serializable) comment);

        detatileFragment.setArguments(bundle);
        return detatileFragment;
    }

    public static DetailsFragment newInstance(Bundle extral) {

        DetailsFragment detatileFragment = new DetailsFragment();

        detatileFragment.setArguments(extral);
        return detatileFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if (bundle != null) {
            mHtmlString = bundle.getString(PARAMS1);
            mBaseParamsDatas = (List<GoodsInfoModel2.Other>) bundle.getSerializable(PARAMS2);
            mCommentdates = (List<CommentModel>) bundle.getSerializable("comment");

        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.details_webview, null);

        return view;
    }

    @Override
    protected void initView() {
        mWebView = (WebView) getView().findViewById(R.id.webview);
        mWebView.getSettings().setBuiltInZoomControls(false);
        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl(url);

        mSwitchLayoutRadio = (RadioGroup) getView().findViewById(R.id.rg_control);
        mSwitchLayoutRadio.setOnCheckedChangeListener(this);

        mIntroductionRadioButton = (RadioButton) getView().findViewById(R.id.rbIntroduction);
        mBaseParamsRadioButton = (RadioButton) getView().findViewById(R.id.rbBaseParams);
        mCommentButton = (RadioButton) getView().findViewById(R.id.rbcomment);

        mIntroductionStub = (ViewStub) getView().findViewById(R.id.vsIntroduction);

        mBaseParamsStub = (ViewStub) getView().findViewById(R.id.vsBaseParams);

        mCommentStub = (ViewStub) getView().findViewById(R.id.vsComment);


        mSwitchLayoutRadio.check(R.id.rbIntroduction);        //默认商品图文选中


    }

    @Override
    protected void initTitle() {
        //super.initTitle();
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.rbIntroduction:

                mIntroductionRadioButton.setTextSize(16);
                mBaseParamsRadioButton.setTextSize(12);
                mCommentButton.setTextSize(12);

                if (mIntroductionView == null) {
                    mIntroductionView = mIntroductionStub.inflate();


                    mWebView = (WebView) mIntroductionView.findViewById(R.id.indroductionWebView);
                    mWebView.getSettings().setBuiltInZoomControls(false);
                    mWebView.getSettings().setDisplayZoomControls(false);
                    mWebView.getSettings().setDomStorageEnabled(true);
                    mWebView.getSettings().setJavaScriptEnabled(true);
                    mWebView.getSettings().setDefaultTextEncodingName(encoding);

                    mWebView.getSettings().setUseWideViewPort(false);
                    mWebView.getSettings().setLoadWithOverviewMode(true);
                    mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);

                    if (!TextUtils.isEmpty(mHtmlString)) {

                        loadDataForWebView(mHtmlString);
                    }

                    mWebView.loadUrl(url);


                } else {

                    mIntroductionView.setVisibility(View.VISIBLE);

                }

                if (mBaseParamsView != null) {
                    mBaseParamsView.setVisibility(View.GONE);
                }
                if (mCommentView != null) {
                    mCommentView.setVisibility(View.GONE);
                }

                break;

            case R.id.rbBaseParams:

                mIntroductionRadioButton.setTextSize(12);
                mBaseParamsRadioButton.setTextSize(15);
                mCommentButton.setTextSize(12);


                if (mBaseParamsView == null) {
                    mBaseParamsView = mBaseParamsStub.inflate();


                    mParamsTableLayout = (TableLayout) mBaseParamsView.findViewById(R.id.tlParaLayout);

                    initParamLayout(mBaseParamsDatas);

                } else {
                    mBaseParamsView.setVisibility(View.VISIBLE);

                }

                if (mIntroductionView != null) {
                    mIntroductionView.setVisibility(View.GONE);
                }
                if (mCommentView != null) {
                    mCommentView.setVisibility(View.GONE);
                }


                break;
            case R.id.rbcomment:
                commentpareserter = new CommentListPreserter(this, getActivity(), goodsid, "0");
                commentpareserter.LoadComment();

                break;

        }


    }


    public void setDataToFragment(String mHtmlString, List<GoodsInfoModel2.Other> datas, String goodsid) {

        this.mHtmlString = mHtmlString;
        this.mBaseParamsDatas = datas;
        this.goodsid = goodsid;

        loadDataForWebView(this.mHtmlString);


    }

    /**
     * 加载数据到webview
     *
     * @param html
     */
    public void loadDataForWebView(String html) {
        if (!TextUtils.isEmpty(html)) {
            String newHtml = html.replaceAll("<img", "<img width=100%");
            if (mWebView == null) {
//				mWebView = (WebView) getView().findViewById(R.id.webView);
            }
//			mWebView.loadDataWithBaseURL(null,newHtml, "text/html", encoding,null);
        }
    }


    /**
     * 实例化 参数布局
     *
     * @param datas
     */
    public void initParamLayout(List<GoodsInfoModel2.Other> datas) {
        if (null == datas)
            return;

        for (GoodsInfoModel2.Other params : datas) {

            TableRow tableRow = new TableRow(getActivity());
            tableRow.setBackgroundColor(Color.WHITE);

            //标题
            TextView title = new TextView(getContext());
            TableRow.LayoutParams lp = new TableRow.LayoutParams(0, DensityUtils.dp2px(getActivity(), 30), 2.0f);
            title.setLayoutParams(lp);
            title.setGravity(Gravity.CENTER | Gravity.RIGHT);
            title.setText(params.attr_name);
            title.setPadding(title.getPaddingLeft(), title.getPaddingTop(), DensityUtils.dp2px(getActivity(), 10.0f), title.getPaddingRight());
            title.setBackgroundResource(R.drawable.cell_shape);

            //内容
            TextView content = new TextView(getContext());
            TableRow.LayoutParams lp2 = new TableRow.LayoutParams(0, DensityUtils.dp2px(getActivity(), 30), 4.0f);
            content.setGravity(Gravity.CENTER | Gravity.LEFT);
            content.setLayoutParams(lp2);
            content.setPadding(DensityUtils.dp2px(getActivity(), 10.0f), content.getPaddingTop(), content.getPaddingTop(), content.getPaddingRight());
            content.setText(params.attr_value);
            content.setBackgroundResource(R.drawable.cell_shape);


            tableRow.addView(title);
            tableRow.addView(content);


            mParamsTableLayout.addView(tableRow);

        }


    }


    @Override
    public void ReCommentList(CommentModel comment) {
        mIntroductionRadioButton.setTextSize(12);
        mBaseParamsRadioButton.setTextSize(12);
        mCommentButton.setTextSize(16);

        if (mCommentView == null) {
            LayoutInflater inflater = LayoutInflater.from(context);
            mCommentView = mCommentStub.inflate();
            LinearLayout ll_com = (LinearLayout) mCommentView.findViewById(R.id.llcommmentLayout);

            for (CommentModel.CommentInfo com : comment.getData()) {
                View View = inflater.inflate(R.layout.comment_item, null);
                TextView tv_username = (TextView) View.findViewById(R.id.tv_comment_username);
                TextView tv_content = (TextView) View.findViewById(R.id.tv_conment_contexnt);
                RatingBar rb_start = (RatingBar) View.findViewById(R.id.rb_start);
                TextView tv_time = (TextView) View.findViewById(R.id.tv_comment_time);

                tv_content.setText(com.getContent());
                tv_time.setText(com.getComment_time());
                tv_username.setText(com.getUser_name());
                rb_start.setRating(Float.valueOf(com.getComment_rank()));

                ll_com.addView(View);

            }


        } else {
            mCommentView.setVisibility(View.VISIBLE);

        }

        if (mIntroductionView != null) {
            mIntroductionView.setVisibility(View.GONE);
        }

        if (mBaseParamsView != null) {
            mBaseParamsView.setVisibility(View.GONE);
        }
    }
}
